#include "app_mode_select.h"
#include "app_settings.h"
#include "app_main.h"
#include "stdio.h"
#include "string.h"
#include "../hal/hal_link.h"

// 全局变量，存储模式设置界面对象
static lv_obj_t *mode_settings_screen;
static lv_obj_t *list;
static lv_style_t style_list;
static lv_style_t style_default;
static lv_style_t style_checked;

uint8_t selet_mode = 0;

struct mode_list_t
{
    char *img;
    char *name;
    lv_obj_t *item_obj;
} mode_list[] = {
    {"A:./icon/uav.png", "uav mode", NULL},
    {"A:./icon/ppm.png", "ppm mode", NULL},
    {"A:./icon/usb.png", "usb mode", NULL},
    {"A:./icon/lrj.png", "Deer mode", NULL},
};

static void list_item_set_style(const char num)
{
    for (int i = 0; i < sizeof(mode_list) / sizeof(struct mode_list_t); i++) {
        lv_obj_remove_style(mode_list[i].item_obj, &style_default, LV_PART_MAIN);
        lv_obj_remove_style(mode_list[i].item_obj, &style_checked, LV_PART_MAIN);

        if (num == i) {
            lv_obj_add_style(mode_list[i].item_obj, &style_checked, LV_PART_MAIN);
        } else {
            lv_obj_add_style(mode_list[i].item_obj, &style_default, LV_PART_MAIN);
        }

        // printf("update\n");
    }
}

static uint8_t list_item_get_index(const char * text)
{
    for (int i = 0; i < sizeof(mode_list) / sizeof(struct mode_list_t); i++) {
        if (!strcmp(text, mode_list[i].name)) {
            selet_mode = i;
        }
    }

    list_item_set_style(selet_mode);

    return selet_mode;
}

// 列表项点击事件回调函数
static void list_item_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * bt = lv_event_get_target(e);
    if(code == LV_EVENT_CLICKED) {
        // 获取列表项文本
        const char * text = lv_list_get_btn_text(list, bt);
        selet_mode = list_item_get_index(text);
    }
}

// 返回设置界面按钮点击事件处理函数
static void back_button_event_cb(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    hal_link->tx_mode(selet_mode);
    
    // 以淡入动画效果切换回设置界面
    lv_scr_load_anim(settings_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

static void show_base(void)
{
    // 返回设置界面按钮
    lv_obj_t *back_button = lv_btn_create(mode_settings_screen);
    lv_obj_set_size(back_button, 50, 50);
    lv_obj_align(back_button, LV_ALIGN_TOP_LEFT, 30, 30);
    lv_obj_add_event_cb(back_button, back_button_event_cb, LV_EVENT_CLICKED, NULL);

    /* 设置按钮样式为透明背景 */
    static lv_style_t style_btn;
    lv_style_init(&style_btn);
    lv_style_set_radius(&style_btn, 10); // 设置按钮样式圆角弧度
    
    // 设置背景完全透明
    lv_style_set_bg_opa(&style_btn, LV_OPA_TRANSP);
    lv_style_set_border_opa(&style_btn, LV_OPA_TRANSP);
    lv_style_set_shadow_opa(&style_btn, LV_OPA_TRANSP);

    // 将样式应用到按钮
    lv_obj_add_style(back_button, &style_btn, 0);

    /* 创建一个标签并添加到按钮上 */
    lv_obj_t * label = lv_label_create(back_button);
    lv_label_set_text(label, LV_SYMBOL_LEFT);

    // 初始化标签样式
    static lv_style_t style_label;
    lv_style_init(&style_label);
    lv_style_set_text_color(&style_label, lv_color_black());
    lv_style_set_text_font(&style_label, &lv_font_montserrat_26);
    lv_obj_add_style(label, &style_label, 0);

    /* 标题 */
    lv_obj_t *title_label = lv_label_create(mode_settings_screen);
    lv_label_set_text(title_label, "Mode Settings");
    lv_obj_align_to(title_label, back_button, LV_ALIGN_OUT_RIGHT_MID, 0, -2);

    static lv_style_t style_label2;
    lv_style_init(&style_label2);
    lv_style_set_text_font(&style_label2, &lv_font_montserrat_18);
    lv_obj_add_style(title_label, &style_label2, 0);
}

static void show_selet_mode(void)
{
    // 定义默认样式
    lv_style_init(&style_default);
    lv_style_set_bg_color(&style_default, lv_color_hex(0xeeeeee));
    lv_style_set_text_color(&style_default, lv_color_hex(0x000000));
    lv_style_set_border_width(&style_default, 1);
    lv_style_set_border_color(&style_default, lv_color_hex(0xCCCCCC));
    lv_style_set_radius(&style_default, 5);
    lv_style_set_height(&style_default, 100); // 设置列表项高度
    lv_style_set_pad_all(&style_default, 10); // 设置内边距

    // 定义选中样式
    lv_style_init(&style_checked);    
    lv_style_set_bg_color(&style_checked, lv_color_hex(0x007BFF));
    lv_style_set_text_color(&style_checked, lv_color_hex(0xFFFFFF));
    lv_style_set_border_width(&style_checked, 1);
    lv_style_set_border_color(&style_checked, lv_color_hex(0x0056b3));
    lv_style_set_radius(&style_checked, 5);
    lv_style_set_height(&style_checked, 100); // 设置列表项高度
    lv_style_set_pad_all(&style_checked, 10); // 设置内边距
    
    // 初始化列表样式
    lv_style_init(&style_list);
    lv_style_set_bg_opa(&style_list, LV_OPA_TRANSP); // 列表背景透明
    lv_style_set_border_width(&style_list, 0); // 无边框
    lv_style_set_pad_all(&style_list, 10); // 内边距

    list = lv_list_create(mode_settings_screen);
    lv_obj_set_size(list, lv_pct(80), lv_pct(70));
    lv_obj_align(list, LV_ALIGN_CENTER, 0, 20);
    lv_obj_add_style(list, &style_list, LV_PART_MAIN);

    for (int i = 0; i < sizeof(mode_list) / sizeof(struct mode_list_t); i++) {
        mode_list[i].item_obj = lv_list_add_btn(list, mode_list[i].img, mode_list[i].name);
        lv_obj_add_event_cb(mode_list[i].item_obj, list_item_event_cb, LV_EVENT_CLICKED, NULL);
    }
}

// 创建模式设置界面
void mode_settings_screen_init(void)
{
    mode_settings_screen = lv_obj_create(NULL);

    show_return_base("Mode Settings", mode_settings_screen, back_button_event_cb);

    show_selet_mode();
}

// 获取模式设置界面对象
lv_obj_t *mode_settings_screen_get(void)
{
    selet_mode = get_show_mode();
    list_item_set_style(selet_mode);
    return mode_settings_screen;
}

/******************* (C) COPYRIGHT 2025 FUYEBING*****END OF FILE************/